package org.algorithms.ricartagrawala;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.UnknownHostException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import org.ActionConstants;
import org.ApplicationException;

@SuppressWarnings("serial")
public class RicartAgrawalaPanelImpl extends JFrame implements WindowListener, RicartAgrawalaPanel {
	
	private JPanel mainPane;
	private ProcessCi[] processes;
	private Controller controller;
	private JTextArea logArea;
	private JButton btnInitControl;
	private JButton btnPauseMove;
	private JButton btnReinitMove;
	private JButton btnReleaseControl;
	private JPanel ctrlPanel;
	private JSplitPane splitPane;
	private int pid;
//	private DateFormat sdf = new SimpleDateFormat("HH:mm:sss");
	
	/**
	 * Create the frame.
	 */
	public RicartAgrawalaPanelImpl() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.setBounds(100, 100, 331, 300);
		setMainPane(new JPanel());
		getMainPane().setBorder(new EmptyBorder(5, 5, 5, 5));
		getMainPane().setLayout(new BoxLayout(getMainPane(), BoxLayout.X_AXIS));
		setContentPane(getMainPane());
		
		setSplitPane(new JSplitPane());
		getSplitPane().setPreferredSize(new Dimension(240, 40));
		getSplitPane().setOrientation(JSplitPane.VERTICAL_SPLIT);
		getMainPane().add(getSplitPane());
		
		setCtrlPanel(new JPanel());
		getSplitPane().setLeftComponent(getCtrlPanel());
		getCtrlPanel().setLayout(new GridLayout(3, 2, 0, 0));
		
		setBtnInitControl(new JButton("Iniciar Controle"));
		getBtnInitControl().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				initControl();
			}
		});
		getCtrlPanel().add(getBtnInitControl());
		
		setBtnPauseMove(new JButton("Pausar Movimento"));
		getBtnPauseMove().setEnabled(false);
		getCtrlPanel().add(getBtnPauseMove());
		
		setBtnReinitMove(new JButton("Reiniciar Movimento"));
		getBtnReinitMove().setEnabled(false);
		getBtnReinitMove().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				initMovement();
			}
		});
		getCtrlPanel().add(getBtnReinitMove());
		
		setBtnReleaseControl(new JButton("Liberar Controle"));
		getBtnReleaseControl().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				releaseControl();
			}
		});
		getBtnReleaseControl().setEnabled(false);
		getCtrlPanel().add(getBtnReleaseControl());
		
		JScrollPane logPanel = new JScrollPane();
		getSplitPane().setRightComponent(logPanel);
		
		setLogArea(new JTextArea());
		getLogArea().setEditable(false);
		getLogArea().setCaretPosition(getLogArea().getDocument().getLength());
		logPanel.setViewportView(getLogArea());
		getBtnPauseMove().addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				pauseMovement();
			}
		});
		addWindowListener(this);
	}
	
//	@Override
//	public void logMessage(String message, boolean logTime) {
//		String oldText = getLogArea().getText();
//		String timeLog = "";
//		if (logTime) timeLog = "     --> HH:mm:sss: " + sdf.format(new Date());
//		getLogArea().setText( message + timeLog + "\n" + oldText);
//	}
	
	@Override
	public void windowOpened(WindowEvent event) {
		AddressDialog dialog = new AddressDialog();
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
		
		try {
			dialog.buildProcessCi();
			setPid(dialog.getPid());
			setProcesses( dialog.getProcesses() );
			
			setController(new ControllerImpl(getPid(), getProcesses()));
			getController().setControlPanel(this);
			setTitle("Panel: " + getPid());
			
			getController().initialSetup();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void initMovement() {
		getController().handleControlPanelAction( ActionConstants.INIT_MOVEMENT );
	}

	@Override
	public void pauseMovement() {
		getController().handleControlPanelAction( ActionConstants.PAUSE_MOVEMENT );
	}

	@Override
	public void initControl() {
		getController().handleControlPanelAction( ActionConstants.INIT_CONTROL );
	}

	@Override
	public void releaseControl() {
		getController().handleControlPanelAction( ActionConstants.RELEASE_CONTROL );
	}
	
	public void paint(Graphics g) {
		super.paint(g);
	}
	
	@Override
	public void windowActivated(WindowEvent arg0) {	
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
	}

	@Override
	public void windowIconified(WindowEvent arg0) {	
	}
	
	/*
	 * GETTERS AND SETTERS
	 */

	public ProcessCi[] getProcesses() {
		return processes;
	}

	public void setProcesses(ProcessCi[] processes) {
		this.processes = processes;
	}

	public Controller getController() {
		return controller;
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}
	public JTextArea getLogArea() {
		return logArea;
	}
	public JButton getBtnInitControl() {
		return btnInitControl;
	}
	public JButton getBtnPauseMove() {
		return btnPauseMove;
	}
	public JButton getBtnReinitMove() {
		return btnReinitMove;
	}
	public JButton getBtnReleaseControl() {
		return btnReleaseControl;
	}

	private JPanel getMainPane() {
		return mainPane;
	}

	private void setMainPane(JPanel mainPane) {
		this.mainPane = mainPane;
	}

	private void setLogArea(JTextArea logArea) {
		this.logArea = logArea;
	}

	private void setBtnInitControl(JButton btnInitControl) {
		this.btnInitControl = btnInitControl;
	}

	private void setBtnPauseMove(JButton btnPauseMove) {
		this.btnPauseMove = btnPauseMove;
	}

	private void setBtnReinitMove(JButton btnReinitMove) {
		this.btnReinitMove = btnReinitMove;
	}

	private void setBtnReleaseControl(JButton btnReleaseControl) {
		this.btnReleaseControl = btnReleaseControl;
	}
	public JPanel getCtrlPanel() {
		return ctrlPanel;
	}

	private void setCtrlPanel(JPanel ctrlPanel) {
		this.ctrlPanel = ctrlPanel;
	}
	public JSplitPane getSplitPane() {
		return splitPane;
	}

	private void setSplitPane(JSplitPane splitPane) {
		this.splitPane = splitPane;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}
}